package com.app.piemoment.view;

import static android.view.ViewGroup.LayoutParams.FILL_PARENT;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.app.piemoment.PieMomentApp;
import com.app.piemoment.R;
import com.app.piemoment.util.Utils;

public class PmDialog extends Dialog {

	private Context context;

	private int id;

	private PmDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init(context);
	}

	private PmDialog(Context context, int theme) {
		super(context, theme);
		init(context);
	}

	public PmDialog(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// linearLayout.setBackgroundResource(R.drawable.bg_toast);
		LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.progress_load, null);
		linearLayout.getBackground().setAlpha(127);
		linearLayout.setGravity(Gravity.CENTER);
		int LPH = Utils.dipToPixels(context, 46);
		setContentView(linearLayout, new ViewGroup.LayoutParams(
				LayoutParams.WRAP_CONTENT, LPH));

		Window window = this.getWindow();
		window.setBackgroundDrawableResource(android.R.color.transparent);
		LayoutParams params = window.getAttributes();// 获取LayoutParams
		window.setGravity(Gravity.LEFT | Gravity.TOP);
		params.x = PieMomentApp.getScreenWidth() / 2
				- Utils.dipToPixels(context, 50);
		params.y = PieMomentApp.getScreenHeight() * 3 / 4;
		window.setAttributes(params);// 设置生效
	}

	@Override
	public void show() {
		super.show();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	private static Dialog mDialog;

	public static void createListView(Context contexts, CharSequence[] items,
			int checkedItem, final OnClickListener listener) {
		ListView listView = new ListView(contexts);
		int layout = R.layout.labi_select_dialog_singlechoice;

		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
				contexts, layout, R.id.text1, items);
		listView.setAdapter(adapter);
		listView.setDivider(new ColorDrawable(Color.parseColor("#d1d1d1")));

		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		if (checkedItem > -1 && checkedItem < items.length) {
			listView.setItemChecked(checkedItem, true);
			listView.setSelection(checkedItem);
		}

		listView.setBackgroundColor(Color.WHITE);
		listView.setCacheColorHint(Color.WHITE);
		if (listener != null) {
			listView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView parent, View v,
						int position, long id) {
					listener.onClick(mDialog, position);
					mDialog.dismiss();
				}
			});
		}

		mDialog = new Dialog(contexts);
		mDialog.setTitle("请选择");
		mDialog.addContentView(listView, new LayoutParams(FILL_PARENT,
				FILL_PARENT));
		mDialog.show();
	}

}
